import { v4 as uuidv4 } from 'uuid';

export function uuid() {
  return uuidv4().replaceAll('-', '');
}

export const getCookie = (key: string) => {
  if (!key) return null;
  const allcookies = document.cookie;
  let cookie_pos = allcookies.indexOf(`${key}=`);
  if (cookie_pos !== -1) {
    cookie_pos += key.length + 1;
    let cookie_end = allcookies.indexOf(';', cookie_pos);
    if (cookie_end === -1) {
      cookie_end = allcookies.length;
    }
    return unescape(allcookies.substring(cookie_pos, cookie_end));
  }
  return null;
};

// 原生eval替代方案
export function cEval(str: string) {
  const FN = Function;
  return new FN('return ' + str)();
}
